using System.IO;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
#if UNITY_IOS
using UnityEditor.iOS.Xcode;
#endif

namespace UnityXEditor
{
    public class WeChatIOSAutoBuild
    {
        public int callbackOrder => 0;
        //构建之前处理
        public void OnPreprocessBuild(BuildReport report)
        {
            switch (report.summary.platform)
            {
                case BuildTarget.Android:
                    AndroidPreprocessBuild(report.summary.outputPath);
                    break;
                case BuildTarget.iOS:
                    iOSPreprocessBuild(report.summary.outputPath);
                    break;
            }
        }
        //构建之后处理
        public void OnPostprocessBuild(BuildReport report)
        {
            switch (report.summary.platform)
            {
                case BuildTarget.Android:
                    AndroidPostprocessBuild(report.summary.outputPath);
                    break;
                case BuildTarget.iOS:
                    iOSPostprocessBuild(report.summary.outputPath);
                    break;
            }
        }
        //构建Android之前处理
        private void AndroidPreprocessBuild(string outputPath)
        {
        }
        //构建iOS之前处理
        private void iOSPreprocessBuild(string outputPath)
        {
        }
        //构建Android之后处理
        private static void AndroidPostprocessBuild(string outputPath)
        {
        }
        //构建iOS之后处理
        private static void iOSPostprocessBuild(string outputPath)
        {
#if UNITY_IOS
            //修改Info.plist文件
            PlistDocument plist = new PlistDocument();
            plist.ReadFromFile(outputPath + "/Info.plist");
            {
                //出口合规证明不使用加密
                plist.root.SetBoolean("ITSAppUsesNonExemptEncryption", false);
                //APP语言
                plist.root.SetString("CFBundleDevelopmentRegion", "zh-Hans");
                //添加权限
                plist.root.SetString("NSPhotoLibraryUsageDescription", "保存拍照和录像");
                //添加白名单
                PlistElementArray queriesSchemes = plist.root.CreateArray("LSApplicationQueriesSchemes");
                queriesSchemes.AddString("weixin");
                queriesSchemes.AddString("weixinULAPI");
                queriesSchemes.AddString("weixinURLParamsAPI");
                //添加微信AppId
                PlistElementArray urlTypes = plist.root.CreateArray("CFBundleURLTypes");
                var urlItem2 = urlTypes.AddDict();
                urlItem2.SetString("CFBundleTypeRole", "Editor");
                urlItem2.SetString("CFBundleURLName", "weixin");
                var schemes2 = urlItem2.CreateArray("CFBundleURLSchemes");
                schemes2.AddString(UnityXSettings.wechat.iosAppId);
            }
            plist.WriteToFile(outputPath + "/Info.plist");

            //修改工程配置文件
            string pbxPath = PBXProject.GetPBXProjectPath(outputPath);
            PBXProject project = new PBXProject();
            project.ReadFromFile(pbxPath);
            {
                string mainGuid = project.GetUnityMainTargetGuid();
                //添加Framework
                project.AddFrameworkToProject(mainGuid, "Security.framework", true);
                project.AddFrameworkToProject(mainGuid, "CoreGraphics.framework", true);
                project.AddFrameworkToProject(mainGuid, "WebKit.framework", true);
                //添加Other Linker Flags "-ObjC -all_load"
                project.AddBuildProperty(mainGuid, "OTHER_LDFLAGS", "-ObjC");
                project.AddBuildProperty(mainGuid, "OTHER_LDFLAGS", "-all_load");

                string frameworkGuid = project.GetUnityFrameworkTargetGuid();
                //添加Framework
                project.AddFrameworkToProject(frameworkGuid, "Security.framework", true);
                project.AddFrameworkToProject(frameworkGuid, "CoreGraphics.framework", true);
                project.AddFrameworkToProject(frameworkGuid, "WebKit.framework", true);
            }
            project.WriteToFile(pbxPath);

            //添加Capability
            //string capabilityPath = "Entitlements.entitlements";
            string capabilityPath = "Unity-iPhone/Unity-iPhone.entitlements";
            ProjectCapabilityManager capabilityManager = new ProjectCapabilityManager(pbxPath, capabilityPath, null, project.GetUnityMainTargetGuid());
            {
                capabilityManager.AddAssociatedDomains(new string[] { "applinks:" + UnityXSettings.wechat.iosApplinks });
                //苹果账号登录
                capabilityManager.AddSignInWithApple();
            }
            capabilityManager.WriteToFile();
#endif
        }
    }
}